#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
using namespace std;

int N;
ll w[maxn];
ll f[405][405][405];

ll solve(){
    memset(f,-1,sizeof f);
    for(int i = 1;i<=N;i++) f[1][i][i] = 0;
    for(int len = 2;len<=N;len++){
        for(int i = 1;i<=N-len+1;i++){
            int j = i+len-1;
            for(int k = i;k<j;k++){
                int lenl = k-i+1,lenr = len-lenl;
                if(f[len][i][j] == -1) f[len][i][j] = f[lenl][i][k] + f[lenr][k+1][j] + w[j] - w[i-1];
                else f[len][i][j] = min(f[len][i][j],f[lenl][i][k] + f[lenr][k+1][j] + w[j] - w[i-1]);
            }
        }
    }
    return f[N][1][N];
}

int main(){
    // debug;
    ios;

    cin>>N;
    for(int i = 1;i<=N;i++) cin>>w[i];
    for(int i = 1;i<=N;i++) w[i] += w[i-1];

    cout<<solve()<<'\n';
    
    return 0;
}
